<?php
namespace App\Http\Controllers\Setting\Template;

use App\Http\Controllers\Controller;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class TemplateStoreController extends Controller
{

    public function index()
    {
        $all = $this->all;
        $pageSize = $all['pageSize'] ?? 20;
        $pageNo = $all['pageNo'] ?? 1;

        try{
            $where = function ($query) use ($all) {
                if (isset($all['openid'])) {
                    $query->where('stl.openid', '=', $all['openid']);
                }
            };

            $total = DB::table('sys_template_log as stl')
                ->where($where)
                ->count();

            $info = DB::table('sys_template_log as stl')
                ->leftJoin('lottery_users as lu','stl.openid','=','lu.openid')
                ->leftJoin('users','stl.send_user','=','users.id')
                ->where($where)
                ->select('stl.log_id','stl.openid','stl.blade_name','stl.method','stl.data','stl.success','stl.path','send_user','stl.created_at','lu.nickname','users.name')
                ->orderByDesc('stl.log_id')
                ->skip(($pageNo - 1) * $pageSize)
                ->take($pageSize)
                ->get()
                ->map(function($item){
                    $item->success_name = $item->success?'是':'否';
                    $item->blade = Data::$wechat_blade_name[$item->blade_name];
                    return $item;
                })
                ->toArray();

            return $this->setReturnJson(0,'success',['total'=>$total,'info'=>$info]);
        }catch (\Exception $e){
            Log::error(TemplateStoreController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'查询失败',['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
        }


    }

}
